//
//  MCPhotoLibraryDetailCell.swift
//  
//
//  Created by 满聪 on 2019/7/4.
//  Copyright © 2019 CocoaPods. All rights reserved.
//

import UIKit

import MCComponentExtension
import Photos

protocol MCPhotoLibraryDetailCellDelegate: NSObjectProtocol {
    func photoLibraryDetailCell(_ cell: MCPhotoLibraryDetailCell, selectItemAtRow row: Int)
}

class MCPhotoLibraryDetailCell: UICollectionViewCell {
    
    
    public weak var delegate: MCPhotoLibraryDetailCellDelegate?
    public var row: Int = 0
    
    public var model: MCPhotoLibraryDetailModel = MCPhotoLibraryDetailModel() {
        didSet {
            
            selectedIcon.isSelected = model.isSelected
            
            if model.asset.mediaType == .video {
                videoImageView.isHidden = false
                timeLabel.isHidden = false
                timeLabel.text = model.asset.duration.toMinutesAndSeconds()
            } else {
                videoImageView.isHidden = true
                timeLabel.isHidden = true
            }
            
            
            
//            let title = model.selectIndex == 0 ? "" : model.selectIndex.stringValue
//            selectedIcon.setTitle(title, for: .normal)
//
            if model.isOpenBarrier && !model.isSelected {
                coverView.isHidden = false
            } else {
                coverView.isHidden = true
            }
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(imageView)
        self.addSubview(selectedIcon)
        self.addSubview(videoImageView)
        self.addSubview(timeLabel)
        self.addSubview(coverView)
        
        
        imageView.frame = CGRect.init(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
        selectedIcon.frame = CGRect.init(x: self.frame.size.width - 36, y: 0, width: 36, height: 36)
        
        
        videoImageView.frame = CGRect.init(x: 5, y: self.frame.size.height - 5 - 18, width: 18, height: 18)

        timeLabel.frame = CGRect.init(x: 10 + 20, y: self.frame.size.height - 5 - 18, width: 40, height: 18)

        
        coverView.frame = CGRect.init(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
        
    }
    
    
    //播放动画，是否选中的图标改变时使用
    func playAnimate() {
        //图标先缩小，再放大
        UIView.animateKeyframes(withDuration: 0.4, delay: 0, options: .allowUserInteraction, animations: {
            UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.2, animations: {
                self.selectedIcon.transform = CGAffineTransform(scaleX: 0.7, y: 0.7)
            })
            UIView.addKeyframe(withRelativeStartTime: 0.2, relativeDuration: 0.4, animations: {
                self.selectedIcon.transform = CGAffineTransform.identity
            })
        }, completion: nil)
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }()
    
    
    lazy var selectedIcon: UIButton = {
        let button = UIButton.init(type: .custom)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button.layer.cornerRadius = 13
        button.layer.masksToBounds = true
        button.setTitleColor(UIColor.white, for: .normal)
        button.isUserInteractionEnabled = false
        
        let notSelImage = Bundle.mc_loadImage("image_not_selected", from: "MCPhotoLibraryBundle", in: "MCComponentFunction")
        button.setBackgroundImage(notSelImage, for: .normal)

        let selImage = Bundle.mc_loadImage("image_selected", from: "MCPhotoLibraryBundle", in: "MCComponentFunction")
        button.setBackgroundImage(selImage, for: .selected)
        return button
    }()
    
    lazy var videoImageView: UIImageView = {
        let iv = UIImageView()
        let image = Bundle.mc_loadImage("videoStatus", from: "MCPhotoLibraryBundle", in: "MCComponentFunction")
        iv.image = image
        return iv
    }()
    
    lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.white
        label.font = UIFont.mc10
        return label
    }()
    
    lazy var coverView: UIButton = {
        let view = UIButton.init(type: .custom)
        view.backgroundColor = UIColor.init(white: 1, alpha: 0.7)
        view.isHidden = true
        view.addTarget(self, action: #selector(coverEvent), for: .touchUpInside)
        return view
    }()
}



extension MCPhotoLibraryDetailCell {
    @objc func coverEvent() {
        delegate?.photoLibraryDetailCell(self, selectItemAtRow: row)
    }
}
